package com.syyo.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syyo.admin.domain.entity.LogEntity;
import com.syyo.admin.domain.req.LogReq;
import com.syyo.admin.mapper.LogMapper;
import com.syyo.admin.service.LogService;
import com.syyo.admin.utils.MyStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author wang
 * @date 2020-09-02 16:38:49
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, LogEntity> implements LogService {

    @Autowired
    private LogMapper logMapper;

    /**
     * 新增
     *
     * @return ResultVo
     */
    @Override
    @Transactional
    public int add(LogEntity log) {
        int insert = logMapper.insert(log);
        return insert;
    }

    /**
     * 详情
     *
     * @return ResultVo
     */
    @Override
    public LogEntity findOne(Integer id) {
        return logMapper.selectById(id);
    }

    /**
     * 列表
     *
     * @return ResultVo
     */
    @Override
    public IPage<LogEntity> findAll(Integer pageNum, Integer pageSize, LogReq req) {
        Page<LogEntity> teacherPage = new Page<LogEntity>(pageNum, pageSize);
        QueryWrapper<LogEntity> wrapper = new QueryWrapper<>();
        if (MyStringUtils.isNotEmpty(req.getLogType())) {
            wrapper.eq("log_type", req.getLogType());
        }
        // 创建时间倒序
        wrapper.orderByDesc("create_time");
        return logMapper.selectPage(teacherPage, wrapper);
    }

}